﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace COFE.Web
{
    /// <summary>
    /// Represent service specific and static information of the current request. 
    /// </summary>
    public static class ServiceConstants
    {

        /// <summary>
        /// Namespace for COFEWS.
        /// </summary>
        public const string Namespace = "http://cofews";

        /// <summary>
        /// Gets or Sets the Host of the current request, e.g. http://localhost:8080
        /// </summary>
        [ThreadStatic]
        public static string Host = WSUtils.GetHost();

        /// <summary>
        /// Gets or sets the User of the current request, it's hard coded currently.
        /// </summary>        
        public static string User { get { return COFEWSShared.CurrentUserServiceProfile.UserName; } }
        
        /// <summary>
        /// Gets User manager of the current User.
        /// </summary>
        public static IUserServiceProfile Profile
        {
            get { return COFEWSShared.CurrentUserServiceProfile; } 
        }

        /// <summary>
        /// Used by LinkTemplate, provide a list of templates for links.
        /// </summary>
        public static class Templates
        {
            /// <summary>
            /// First one is the text to replace in LinkTemplate (lookupText), Second one is the text to replace.
            /// </summary>
            public static Dictionary<string, string> GlobalLookupDictionary = new Dictionary<string, string>()
            {
               { "#host#", "#host#" },
               { "#entries#", "entries" },            
               { "#entrytypes#", "entrytypes" },   
               { "#search#", "search" },   
               { "#lookup#", "lookup" },
               { "#events#", "events" }
            };

            /// <summary>
            /// Template for an entry.
            /// </summary>
            public static string Entry = "/#entries#/#id#";

            /// <summary>
            /// Template for entry type.
            /// </summary>
            public static string EntryEntryType = "/#entrytypes#/#typeid#";

            /// <summary>
            /// Template for metadata of an entry.
            /// </summary>
            public static string EntryMetadata = "/#entries#/#id#/metadata";

            /// <summary>
            /// Template for a List of entries.
            /// </summary>
            public static string EntryList = "/#entries#/#id#/list";

            /// <summary>
            /// Template for an entry's stream.
            /// </summary>
            public static string EntryStream = "/#entries#/#id#/stream";

            /// <summary>
            /// Template for entry type.
            /// </summary>
            public static string EntryType = "/#entrytypes#/#id#";

            ///// <summary>
            ///// Template for icon stream (as png).
            ///// </summary>
            //public static string EntryIcon = "/#entries#/#id#/icon";

            public static string EntryTypeIcon = "/#entrytypes#/#id#/icon";

            public static string BaseSearch = "/#search#?#ss#";

            /// <summary>
            /// Template for a search.
            /// </summary>
            public static string Search = BaseSearch + "#curpage#";
            //public static string SearchStart = "/#search#/#id#/start"; //use result to start instead.

            /// <summary>
            /// Template for previous search.
            /// </summary>
            public static string PrevSearch = BaseSearch + "#prevpage#";

            /// <summary>
            /// Template for next search.
            /// </summary>
            public static string NextSearch = BaseSearch + "#nextpage#";
            

            /// <summary>
            /// Template for result of a search.
            /// </summary>
            public static string SearchResult = "/#search#/#id#/result";

            /// <summary>
            /// Template for a event list.
            /// </summary>
            public static string EventList = "/#events#/#id#";

            /// <summary>
            /// Template for tomorrow's event list.
            /// </summary>
            public static string NextEventList = "/#events#/#nextid#";

            /// <summary>
            /// Template for yesterday's event list.
            /// </summary>
            public static string PrevEventList = "/#events#/#previd#";

            /// <summary>
            /// Format link template uri using macros (e.g. #id#).
            /// </summary>
            /// <param name="template"></param>
            /// <param name="lookupDic"></param>
            /// <returns></returns>
            public static string FormatTemplate(string template, Dictionary<string, string> lookupDic)
            {
                template = template.Replace("#host#", ServiceConstants.Host);
                foreach (var key in GlobalLookupDictionary.Keys)
                    template = template.Replace(key, GlobalLookupDictionary[key]);
                foreach (var key in lookupDic.Keys)
                    template = template.Replace(key, lookupDic[key]);
                return template;
            }
        }
    }
}
